package com.wenhao.newcode.shopee;

import java.util.Scanner;

/**
 * @author Wenhao Tong
 * @create 2021-07-17 13:38
 */
public class Main4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String[] s1 = s.split(" ");
        int[] nums = new int[s1.length];
        for (int i = 0;i < s1.length;i++) {
            nums[i] = Integer.parseInt(s1[i]);
        }
        Main4 main4 = new Main4();
        main4.getLeaf(0,nums.length - 1,nums);
    }

    private void getLeaf(int left,int right,int[] nums) {
        if (left > right) {
            return;
        }
        if (left== right) {
            System.out.print(nums[left] + " ");
        }
        int root = nums[left];
        int rightroot = left + 1;
        while (rightroot <= right) {
            if (nums[rightroot] > root) {
                break;
            }
            rightroot++;
        }
        getLeaf(left + 1,rightroot - 1,nums);
        getLeaf(rightroot,right,nums);
    }
}
